require "gadgets/combobox"
require "map_editor/io"
class MapConfigDialog
   def self.show(base, options, &func)
      dialog = Gtk::Dialog.new('Interactive Dialog',
			       base,
			       Gtk::Dialog::MODAL |
			       Gtk::Dialog::DESTROY_WITH_PARENT,
			       ["OK", Gtk::Dialog::RESPONSE_OK],
			       ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
			       )
            
      dialog.window_position = Gtk::Window::POS_MOUSE
             
      main_box = Gtk::HBox.new(false, 0)
      main_box.set_border_width(8)
      dialog.vbox.pack_start(main_box, false, false, 0)

      stock = Gtk::Image.new(Gtk::Stock::DIALOG_QUESTION, Gtk::IconSize::DIALOG)
      main_box.pack_start(stock, false, false, 0)

      table = Gtk::Table.new(2, 6, false)
      table.set_row_spacings(4)
      table.set_column_spacings(4)
      main_box.pack_start(table, true, true, 0)
      
      # Show Map ID
      hbox = Gtk::HBox.new
      id_label = Gtk::Label.new("", true)
      table.attach_defaults(Gtk::Label.new('ID', true), 0, 1, 0, 1)
      table.attach_defaults(id_label, 1, 2, 0, 1)
      
       # ID Input
      label, name_input = MapConfigDialog.create_name_input
      table.attach_defaults(label, 0, 1, 1, 2)
      table.attach_defaults(name_input, 1, 2, 1, 2)
      
      # Map Size Input
      label, widget, map_x_input, map_y_input = MapConfigDialog.create_map_size_inputs
      table.attach_defaults(label, 0, 1, 2, 3)
      table.attach_defaults(widget, 1, 2, 2, 3)
      
      # Base Chip Selection
      label, base_chip_combo, base_chipset_list = MapConfigDialog.create_base_chipset_list
      table.attach_defaults(label, 0, 1, 3, 4)
      table.attach_defaults(base_chip_combo, 1, 2, 3, 4)
      
      # Anime Chip Selection
      label, anime_chip_combo, anime_chipset_list = MapConfigDialog.create_anime_chipset_list
      table.attach_defaults(label, 0, 1, 4, 5)
      table.attach_defaults(anime_chip_combo, 1, 2, 4, 5)
      
      # Panorama Selection
      label, panorama_combo, panorama_list = MapConfigDialog.create_panorama_list
      table.attach_defaults(label, 0, 1, 5, 6)
      table.attach_defaults(panorama_combo, 1, 2, 5, 6)
      
      if options
        id_label.text = options[:map_id].to_s if options[:map_id]
        name_input.text = options[:name] ? options[:name] : options[:map_id]
        map_x_input.value = options[:width] if options[:width]
        map_y_input.value = options[:height] if options[:height]

        base_chip_combo.select(options[:base_chipset])
        anime_chip_combo.select(options[:anime_chipset])
        panorama_combo.select(options[:panorama])
      end

      main_box.show_all
      
      response = dialog.run
      
      if response == Gtk::Dialog::RESPONSE_OK
        func.call(name_input.text, map_x_input.value.floor, map_y_input.value.floor, base_chip_combo.selected_data, anime_chip_combo.selected_data, panorama_combo.selected_data)
      end
      dialog.destroy
    end
    
    def self.create_base_chipset_list
      list = MapEditor::IO.get_base_chipset_list
      combobox = ::Gadgets::Combobox.new(list, [String, String])
      return  Gtk::Label.new('Base chipset', true), combobox, list
    end

    def self.create_anime_chipset_list
      list = MapEditor::IO.get_anime_chipset_list
      combobox = ::Gadgets::Combobox.new(list, [String, String])
      return  Gtk::Label.new('Anime chipset', true), combobox, list
    end
    
    def self.create_panorama_list
      list = MapEditor::IO.get_panorama_list
      combobox = ::Gadgets::Combobox.new(list, [String, String])
      return  Gtk::Label.new('Panorama', true), combobox, list
    end

    
    def self.create_name_input
      label = Gtk::Label.new('Name', true)

      name_input = Gtk::Entry.new
      return label, name_input
    end
    
    def self.create_map_size_inputs
      label = Gtk::Label.new('Map Size', true)
          
      hbox = Gtk::HBox.new
      
      # Map X Input
      map_x_input = Gtk::SpinButton.new(10, 99, 1)
      map_x_input.width_request = 40
      map_x_input.text = "10"
      hbox.add(map_x_input)

      hbox.add(Gtk::Label.new('X', true))
      
      # Map Y Input
      map_y_input = Gtk::SpinButton.new(10, 99, 1)
      map_y_input.width_request = 40
      map_y_input.text = "10"
      hbox.add(map_y_input)
      
      return label, hbox, map_x_input, map_y_input
    end
end